{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b5054735",
   "metadata": {},
   "source": [
    "### TAD boundaries shared across cell lines "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "991cef43",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path \n",
    "from pybedtools import BedTool\n",
    "from io import StringIO\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c60cd2fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "wkdir=\"/lustre/scratch126/humgen/projects/interval_rna/interval_rna_seq/thomasVDS/misexpression_v3\"\n",
    "wkdir_path=Path(wkdir)\n",
    "\n",
    "tad_boundaries_dir = wkdir_path.joinpath(\"reference/4d_nucleome\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1a1dd6e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "imr90\n",
      "huvec\n",
      "hnek\n",
      "hmec\n"
     ]
    }
   ],
   "source": [
    "### generate set of shared TAD boundaries in GM12878\n",
    "outdir_path = tad_boundaries_dir.joinpath(\"shared_boundaries\")\n",
    "outdir_path.mkdir(parents=True, exist_ok=True)\n",
    "input_path = tad_boundaries_dir.joinpath(\"gm12878_hi_c/tad_boundaries/4DNFIVK5JOFU.bed\")\n",
    "# select boundaries in GM12878 cells that overlap boundaries +/-50kb in other cell lines \n",
    "cell_line_list = [\"imr90\", \"huvec\", \"hnek\", \"hmec\"]\n",
    "id_path_list = [\"4DNFIMNT2VYL\", \"4DNFI9MZWZF7\", \"4DNFICLU9GUP\", \"4DNFIJL18YS3\"]\n",
    "for cell_line, file_id in zip(cell_line_list, id_path_list):\n",
    "    print(cell_line)\n",
    "    input_bed = BedTool(input_path).sort()\n",
    "    output_path = outdir_path.joinpath(input_path.name.split(\".bed\")[0] + f\"_{cell_line}.bed\")\n",
    "    tad_boundaries_path = tad_boundaries_dir.joinpath(f\"{cell_line}_hi_c/tad_boundaries/{file_id}.bed\")\n",
    "    tad_boundaries_bed = BedTool(tad_boundaries_path).sort()\n",
    "    shared_boundaries_str = StringIO(str(input_bed.window(tad_boundaries_bed, w=50000)))\n",
    "    shared_boundaries_df = pd.read_csv(shared_boundaries_str, sep=\"\\t\", header=None)\n",
    "    shared_boundaries_df[[0, 1, 2, 3, 4]].to_csv(output_path, sep=\"\\t\", header=None, index=False)\n",
    "    input_path = output_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd7fc805",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
